<?php

namespace App\Http\Controllers\Api\V1\Wechat;

use App\Http\Controllers\ApiBaseController;
use App\Libs\Commons\MyMessage;
use App\Services\Wechat\MiniProgramService;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Validator;

class EncryptorController extends ApiBaseController
{
	// 消息解密
	public function decrypt(Request $request)
	{
		$param = $request->all();
		$validator = Validator::make($param, [
			'iv' => 'required',
			'data' => 'required',
			'session' => 'required'
		]);

		if ($validator->fails()) {
			return $this->fail(MyMessage::ERROR, $validator->errors()->first());
		}

		$app = MiniProgramService::getInstance();
		$result = $app->encryptor->decryptData($param['session'], $param['iv'], $param['data']);
		return $this->success($result);
	}
}
